//
//  MHAuthorListViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/30.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHAuthorListViewController.h"
#import "MHComicIntroduct.h"
#import "MHComicDetailViewController.h"
#import "MHComicIntroductionCell.h"
#import <AFNetworking.h>

@interface MHAuthorListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *bookListArray;

@end

@implementation MHAuthorListViewController

- (void)acquireData
{
    NSDictionary *para = [NSDictionary dictionaryWithObjects:@[[NSString stringWithFormat:@"%ld", self.bookId], @"linkbook", self.bookAuthor] forKeys:@[@"bookid", @"method", @"author"]];
    [[AFHTTPRequestOperationManager manager]GET:@"http://112.124.96.190:9090/manhuashenqi3yuansen/ComicHandle.ashx" parameters:para success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        self.bookListArray = [NSMutableArray array];
        for (NSDictionary *dict in responseObject) {
            MHComicIntroduct *comicIntroduct = [MHComicIntroduct modelWithDict:dict];
            [self.bookListArray addObject:comicIntroduct];
        }
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self acquireData];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat margin = 20;
    CGFloat padding = 10;
    NSInteger row = 3;
    CGFloat itemW = (Width - 2 * padding - (row - 1) * padding) / row;
    CGFloat itemH = (itemW - padding) * 11.0 / 9.0 + 35;
    flowLayout.itemSize = CGSizeMake(itemW, itemH);
    flowLayout.sectionInset = UIEdgeInsetsMake(margin, padding, margin, padding);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Width, Height - 40) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"MHComicIntroductionCell" bundle:nil] forCellWithReuseIdentifier:@"author"];
    [self.view addSubview:self.collectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.bookListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicIntroductionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"author" forIndexPath:indexPath];
    cell.comicIntroduct = self.bookListArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicDetailViewController *comicDetailVC = [[MHComicDetailViewController alloc]init];
    comicDetailVC.comicIntroduct = self.bookListArray[indexPath.row];
    [self.navigationController pushViewController:comicDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
